﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.Mvc;
using DevExpress.Utils;
using System.Web.UI.WebControls;
namespace Webcar.Controllers
{
    public partial class TableController : BaseControler
    {
        public ActionResult GpsList()
        {
            return View();
        }
        public ActionResult GpsListPartialView(string id)
        {
            return PartialView();
        }

        public ActionResult ExportGpsSummary(string start_date, string end_date)
        {
            if (start_date.IsNull())
            {
                ViewBag.StartDate = DateTime.Now.ToString("yyyy-MM");
                ViewBag.EndDate = DateTime.Now.ToString("yyyy-MM");

                return View();
            }
            DataTable table = GetSummmaryData(start_date, end_date);

            GridViewSettings settings = new GridViewSettings();
            settings.Name = "gridView";
            var deptcol = settings.Columns.Add("deptname", "单位");
            deptcol.CellStyle.Wrap = DefaultBoolean.False;
            deptcol.Width = Unit.Pixel(200);
            deptcol.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("total_gps", "GPS车载<br>终端总数(最大)").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_car_num", "超速报警<br>车辆台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_num", "超速报<br>警次数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_car_num", "越界报警<br>车辆台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_stop_num", "超速被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_stop_num", "越界被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

            settings.Columns.Add("cf_num", "违规被<br>处理台/人").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cf_zj_num", "违规兼<br>职人数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_stop_num", "越界被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_gps").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "cs_car_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "cs_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "yj_car_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "cs_stop_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "yj_stop_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "cf_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "cf_zj_num").DisplayFormat = "n0";
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "yj_stop_num").DisplayFormat = "n0";
            settings.Settings.ShowFooter = true;

            return GridViewExtension.ExportToXls(settings, table, "车辆GPS汇总统计表");

        }
        public ActionResult GpsListExport()
        {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "gridView";
            settings.KeyFieldName = "dept_code,ny";
            settings.Columns.Add("dept_name", "单位").CellStyle.Wrap = DefaultBoolean.False;
            settings.Columns.Add("ny", "月份").CellStyle.Wrap = DefaultBoolean.False;
            settings.Columns.Add("total_gps", "GPS车载<br>终端总数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_car_num", "超速报警<br>车辆台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_num", "超速报<br>警次数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_car_num", "越界报警<br>车辆台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cs_stop_num", "超速被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_stop_num", "越界被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

            settings.Columns.Add("cf_num", "违规被<br>处理台/人").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("cf_zj_num", "违规兼<br>职人数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("note", "备注").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("yj_stop_num", "越界被<br>制止台数").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

            var data = UserDataProvider.GetGpsList(this.RouteData).ToDataTable();
            return GridViewExtension.ExportToXls(settings, data, "车辆GPS统计表");
        }
        public ActionResult GpsDelete(string dept_code, string ny)
        {
            using (var DB = new WebcarContext())
            {
                var model = DB.bs_gps_summary.Find(dept_code, ny);
                if (model != null)
                {
                    DB.bs_gps_summary.Remove(model);
                    DB.SaveChanges();
                    return Success("GpsList", "Table", new { id = model.DEPT_CODE });
                }
                else
                {
                    return Success("GpsList", "Table");
                }
            }
            
        }
        [HttpGet]
        public ActionResult GpsEdit(string dept_code,string ny)
        {
            bs_gps_summary model = null;
            if (dept_code.IsNotNull())
            {
                model = WebcarDataProvider.DB.bs_gps_summary.Find(dept_code,ny);
            }
            if (model == null)
            {
                model = new bs_gps_summary();
                model.NY = DateTime.Now.ToString("yyyy-MM");
                model.TOTAL_GPS = 0;
                model.YJ_CAR_NUM = 0;
                model.YJ_NUM = 0;
                model.YJ_STOP_NUM = 0;
                model.CF_NUM = 0;
                model.CF_ZJ_NUM = 0;
                model.CS_CAR_NUM = 0;
                model.CS_NUM = 0;
                model.CS_STOP_NUM = 0;
            }

            return View(model: model);
        }
        [HttpPost]
        public ActionResult GpsEdit(bs_gps_summary model)
        {
            string error = "";
            if (CheckGpsModel(model, ref error) == false)
            {
                ViewBag.ErrorMessage = error;
                return View(model: model);
            }
            using (var DB = new WebcarContext())
            {
                bs_gps_summary item = DB.bs_gps_summary.Find(model.DEPT_CODE, model.NY);
                if (item == null)
                {
                    DB.bs_gps_summary.Add(model);
                }
                else
                {
                    model.CopyPropertyTo(item);
                }
                DB.SaveChanges();
            }
            return Success("GpsList", "Table", new { id = model.DEPT_CODE });
        }

        bool CheckGpsModel(bs_gps_summary model, ref string error)
        {
            if (model.NY.IsNull())
            {
                error = "请输入月份。";
                return false;
            }
            if ((model.NY+"-01").IsDate()==false)
            {
                error = "请输入正确的月份格式。";
                return false;
            }

            if (model.DEPT_CODE.IsNull())
            {
                error = "请选择部门。";
                return false;
            }

            return true;
        }

        DataTable GetSummmaryData(string start_date, string end_date)
        {
            string sql = @"select max(a.total_gps) total_gps,sum(a.cs_car_num) cs_car_num,sum(a.cs_num) cs_num,sum(a.yj_car_num) yj_car_num,sum(a.yj_num) yj_num,
                            sum(a.cs_stop_num) cs_stop_num,sum(a.yj_stop_num) yj_stop_num,sum(a.cf_num) cf_num,sum(a.cf_zj_num) cf_zj_num
                            ,b.deptname from bs_gps_summary a,sys_dept b 
                            where a.dept_code=b.deptcode and b.use_flag='T'
                            and a.ny between '{0}' and '{1}'
                            group by b.deptname".FormatWith(start_date, end_date);
            DataTable table = WebcarDataProvider.DB.ExecuteDataTable(sql);
            return table;
        }

        public ActionResult GpsSummary(string start_date, string end_date)
        {
            if (start_date.IsNull())
            {
                ViewBag.StartDate = DateTime.Now.ToString("yyyy-MM");
                ViewBag.EndDate = DateTime.Now.ToString("yyyy-MM");

                return View();
            }
            DataTable table = GetSummmaryData(start_date, end_date);

            return View(model: table);
        }
    }
}